package com.mystnihon.freeremote.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class M3UParser {

    public M3UParser() throws Exception {

    }

    public M3UHolder parseFile(String stream) throws FileNotFoundException {
        stream = stream.replaceAll("#EXTM3U", "").trim();
        String[] arr = stream.split("#EXTINF.*,");
        String urls = "", data = "";
        // clean
        {
            for (int n = 0; n < arr.length; n++) {
                if (arr[n].contains("http")) {
                    String nu = arr[n].substring(arr[n].indexOf("http://"), arr[n].indexOf(".mp3") + 4);

                    urls = urls.concat(nu);
                    data = data.concat(arr[n].replaceAll(nu, "").trim()).concat("&&&&");
                    urls = urls.concat("####");
                }
            }
        }
        return new M3UHolder(data.split("&&&&"), urls.split("####"));
    }

    public class M3UHolder {
        private String[] mData, mUrl;

        public M3UHolder(String[] names, String[] urls) {
            mData = names;
            mUrl = urls;
        }

        int getSize() {
            if (mUrl != null)
                return mUrl.length;
            return 0;
        }

        String getName(int n) {
            return mData[n];
        }

        String getUrl(int n) {
            return mUrl[n];
        }
    }
}
